package com.u.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.u.character.Role;
import com.u.message.Message;
import com.u.tool.MysqlUtil;

import net.sf.json.JSONArray;

/**
 * Servlet implementation class ForgetPassword
 */
@WebServlet("/ForgetPassword")
public class ForgetPassword extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ForgetPassword() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = format.format(new Date());
		String value = request.getParameter("value");
		Message message = new Message();
		MysqlUtil db = new MysqlUtil();
		JSONArray json = new JSONArray();
		String messageType = request.getParameter("messageType");
		
		String sql = "select * from y_r.role where user_acc='" + value+"'";
		db.connection();
		List<Role> list = db.query(Role.class, sql);
		if(list.size() == 1) {
			message.setDate(date);
			message.setMessage("ForgetPassword:" + value);
			message.setProcessed("false");
			db.connection();
			db.add(message);
			json.add("success");
		}else {
			json.add("failure");
		}
	
	PrintWriter out = response.getWriter();
	out.println(json.toString());
	out.close();
	
	}

}
